package com.san.InterfManage.controller;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.InterfManage.domain.InterfExamineDo;
import com.san.InterfManage.domain.InterfManageDo;
import com.san.InterfManage.service.InterfExamineService;
import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.system.domain.UserDO;
import com.san.system.service.UserService;
/**
 * 接口申请管理
 * @author Zhangye_hz
 * @date 2020年3月18日
 */
@RequestMapping("/interf/examine")
@Controller
public class InterfExamineController extends BaseController {

	private static final Logger logger=LoggerFactory.getLogger(InterfExamineController.class);
	@Autowired
	InterfExamineService interfExamineService;
	@Autowired
	UserService userService;
	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "interface/examine";
	
	@RequiresPermissions("interf:examine:view")
	@GetMapping("")
	String management(Model model) {
		return PREFIX+"/view";
		
	}
	/**
	 * 申请接口列表
	 * 
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@ResponseBody
	public PageUtils findListSource(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		UserDO userDO ;
		try {
			userDO = (UserDO) userService.getById(getUser().getUserId());
			Query query = new Query(params);
			Long userId = userDO.getUserId();
			query.put("userId", userId);
			pageUtils = interfExamineService.findListSource(query);	
		} catch(Exception e) {
			logger.error("分页获取申请接口列表出错", e);
		}
		return pageUtils;
	}
	/**
	 * 申请接口页面
	 * @param model
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("interf:add")
	String add(Model model) {
		return PREFIX + "/add";
	}
	/**
	 * 申请接口
	 * @param interfManageDO
	 * @return
	 */
	@Log("申请接口")
	@PostMapping("/examine")
	@ResponseBody
	R save(InterfExamineDo interfExamineDo) {
		UserDO userDO ;
		
		try {
			userDO = (UserDO) userService.getById(getUser().getUserId());
			String userId = userDO.getUserId().toString();
			String userName = userDO.getUserName();
			interfExamineDo.setUserId(userId);
			interfExamineDo.setUserName(userName);
			
			if (interfExamineService.add(interfExamineDo) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("申请接口出错", e);
			return R.error();
		}
		return R.error();
	}
	/**
	 * 查询未申请的接口列表
	 * 
	 * @param params
	 * @return
	 */
	@GetMapping("/queryinterf")
	@ResponseBody
	public PageUtils queryinterf(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		UserDO userDO ;
		try {
			userDO = (UserDO) userService.getById(getUser().getUserId());
			Query query = new Query(params);
			Long userId = userDO.getUserId();
			query.put("userId", userId);
			pageUtils = interfExamineService.queryinterf(query);	
		} catch(Exception e) {
			logger.error("分页获取未申请接口列表出错", e);
		}
		return pageUtils;
	}
	/**
	 * 审核页面
	 * @param model
	 * @return
	 */
	@RequiresPermissions("interf:examine:verify")
	@GetMapping("/queryverify")
	String queryverify(Model model) {
		return PREFIX+"/verify";
		
	}
	/**
	 * 查询待审核的接口列表
	 * 
	 * @param params
	 * @return
	 */
	@GetMapping("/verify")
	@ResponseBody
	public PageUtils verify(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		try {
			Query query = new Query(params);
			pageUtils = interfExamineService.verify(query);	
		} catch(Exception e) {
			logger.error("分页获取申请接口列表出错", e);
		}
		return pageUtils;
	}

	/**
	 * 审核接口状态 
	 * @param manageId
	 * @param interfState
	 * @return
	 */
	@PostMapping(value = "/update")
	@RequiresPermissions("interf:examine:update")
	@ResponseBody
	public R update(Long id,Long examineState ) {
		String label = "";
		if (examineState == 1) {
			label = "通过";
		} else if (examineState == 2) {
			label = "不通过";
		}
		InterfExamineDo interfExamineDo = new InterfExamineDo();
		interfExamineDo.setId(id);
		interfExamineDo.setExamineState(examineState);
		try {
			interfExamineService.update(interfExamineDo);
			return R.ok("审核" + label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("审核" + label + "失败");
	}	
	
	/**
	 * 退订接口 
	 * @param manageId
	 * @param interfState
	 * @return
	 */
	@PostMapping(value = "/changeStatus")
	@RequiresPermissions("interf:examine:changeStatus")
	@ResponseBody
	public R changeStatus(Long id ) {
		String label = "退订";

		InterfExamineDo interfExamineDo = new InterfExamineDo();
		interfExamineDo.setId(id);
		try {
			interfExamineService.changeStatus(interfExamineDo);
			return R.ok(label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok( label + "失败");
	}
}
